package com.controller;

import com.entity.Admins;

import com.entity.News;
import com.entity.Projects;
import com.service.*;
import com.util.PageUtil;
import com.util.UploadUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Objects;

/**
 * @author james-CAO
 * @title: AdminController
 * @projectName csm_emall_ssm
 * @description: 后台控制器
 * @date 2020/10/11 22:06
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminsService adminService;

    @Autowired
    private NewService newService;

    @Autowired
    private ProjectService projectService;


    /**
     * 管理员登录
     * @return
     */
    @GetMapping("/login")
    public String log(){
        return "/admin/login.jsp";
    }

    /**
     * 用户登录
     * @param request
     * @param session
     * @return
     */
    @PostMapping("/login")
    public String login(String username,String password, HttpServletRequest request, HttpSession session){
        Admins admin=adminService.getByAdminNameAndPassword(username,password);
        if(Objects.nonNull(admin)){
            session.setAttribute("admin",admin);
            return "redirect:index";
        }
        request.setAttribute("msg","用户名或者密码错误");
        return "/admin/login.jsp";
    }

    @GetMapping("/index")
    public String index(HttpServletRequest request){
        request.setAttribute("msg","欢迎回来用户   ");
        request.setAttribute("me","请进入文宇后台......");
        return "/admin/index.jsp";
    }

    @GetMapping("/adminList")
    public String adminList(HttpServletRequest request){
        request.setAttribute("flag",5);
        request.setAttribute("adminList",adminService.getList());
        return "/admin/admin_list.jsp";
    }

    /**
     * 管理员添加
     */
    @GetMapping("/adminAdd")
    public String adminAdd(HttpServletRequest request){
        request.setAttribute("flag",5);
        return "/admin/admin_add.jsp";
    }

    /**
     * 添加管理
     */
    @PostMapping("/adminSave")
    public String adminSave(String username , String password){
        adminService.adminAdd(username,password);
        return "redirect:adminList";
    }

    /**
     * 管理原用户删除
     */
    @GetMapping("/adminDelete")
    public String adminDelete(int id){
        adminService.delete(id);
        adminService.alter();
        return "redirect:adminList?flag=5";
    }

    /**
     * 管理员-重置密码
     */
    @GetMapping("/adminRe")
    public String adminRe(int id,HttpServletRequest request){
        request.setAttribute("flag",5);
        request.setAttribute("admin",adminService.get(id));
        return "/admin/admin_reset.jsp";
    }
    @PostMapping("/adminReset")
    public String adminReset(int id , String password,HttpServletRequest request,
                             @RequestParam(required = false,defaultValue = "1")int page){
        adminService.update(id,password);
        return "redirect:adminList?page="+page;
    }

    /**
     * 退出功能
     */
    @GetMapping("/logout")
    public String logout(HttpSession session){
//        session.invalidate();  //杀掉回话
        session.removeAttribute("admin");
        return "/admin/login.jsp";
    }

//    @GetMapping("/typeList")
//    public String typeList(HttpServletRequest request){
//        return "/admin/type_list.jsp";
//
//    }


    /**
     * 新闻模块
     * @param request
     * @return
     */

    /**
     * 查询新闻
     * @param request
     * @return
     */
    @GetMapping("/goodList")
//    public String goodList(HttpServletRequest request,
//                           @RequestParam(required = false,defaultValue = "0") byte type,
//                           @RequestParam(required = false,defaultValue = "1") int page,
//                           @RequestParam(required = false,defaultValue = "10") int size){
//        request.setAttribute("flag",2);
//        request.setAttribute("page",page);
//        request.setAttribute("type",type);
//        if (type > 0){
//            String goodIds =topService.getGoodIdsByType(type);
//            request.setAttribute("goodList",goodService.goodListByIds(goodIds,page,size));
//            request.setAttribute("pageTool", PageUtil.getPageTool(request,goodService.getCountByIds(goodIds),page,size));
//        }else {
//            request.setAttribute("goodList",goodService.getList(page,size));
//            request.setAttribute("pageTool",PageUtil.getPageTool(request,goodService.getCount(),page,size));
//        }
//        return "/admin/good_list.jsp";
//    }
    public String goodList(HttpServletRequest request,
                           @RequestParam(required = false, defaultValue = "0") int id,
                           @RequestParam(required = false, defaultValue = "1") int page,
                           @RequestParam(required = false, defaultValue = "5") int size
                           ){
        request.setAttribute("flag",2);
        request.setAttribute("goodList",newService.getList(page,size));
        request.setAttribute("pageTool", PageUtil.getPageTool(request, newService.getCount(), page, size));
        return "/admin/good_list.jsp";
    }

    /**
     * 添加新闻(页面显示)
     *
     * @param request
     * @return
     */
    @GetMapping("/goodAdd")
    public String goodAdd(HttpServletRequest request){
        return "/admin/good_add.jsp";
    }

    /**
     * 添加新闻添加
     */
    @PostMapping("/goodSave")
    public String goodSave(News news, MultipartFile file,
                           @RequestParam(required = false,defaultValue = "1") int page) throws Exception {
        news.setCover(UploadUtil.upload(file));
        newService.add(news);
        return "redirect:goodList?flag=2&page="+page;
    }

    /**
     * 新闻删除
     */
    @GetMapping("/goodDelete")
    public String newsDelete(int id){
        newService.delete(id);
        newService.alter();
        return "redirect:goodList?flag=2";
    }

    /**
     * 新闻修改
     */
    @GetMapping("/goodEdit")
    public String newEdit(HttpServletRequest request,int id){
        request.setAttribute("flag", "2");
        request.setAttribute("news", newService.getId(id));
        return "/admin/good_edit.jsp";
    }

    /**
     * 新闻修改实现
     */
    @PostMapping("/goodUpdate")
    public String newUpdate(News news,MultipartFile file,
                            @RequestParam(required = false,defaultValue = "1")int page) throws Exception {
        if (Objects.nonNull(file) && !file.isEmpty()){
            news.setCover(UploadUtil.upload(file));
        }
        newService.update(news);
        return "redirect:goodList?flag=2&page"+page;
    }


    /**
     * 项目显示
     */
    @GetMapping("/typeList")
    public String typeList(HttpServletRequest request,
                           @RequestParam(required = false, defaultValue = "0") int id,
                           @RequestParam(required = false, defaultValue = "1") int page,
                           @RequestParam(required = false, defaultValue = "5") int size
                           ){
        request.setAttribute("flag",1);
        request.setAttribute("typeList",projectService.getList(page,size));
        request.setAttribute("pageTool", PageUtil.getPageTool(request, projectService.getCount(), page, size));
        return "/admin/type_list.jsp";
    }


    /**
     * 添加（查看页面）
     */
    @GetMapping("/typeAdd")
    public String typeAdd(HttpServletRequest request){
        request.setAttribute("flag",1);
        return "/admin/type_add.jsp";
    }

    /**
     * 添加（具体实现）
     */
    @PostMapping("/typeSave")
    public String typeSave(Projects projects, MultipartFile file,
                           @RequestParam(required = false,defaultValue = "1") int page) throws Exception {
        projects.setXmimg(UploadUtil.upload(file));
        projectService.add(projects);
        return "redirect:typeList?flag=1&page="+page;
    }

    /**
     * 项目删除
     */
    @GetMapping("/typeDelete")
    public String projectDelete(int id){
        projectService.delete(id);
        projectService.alter();
        return "redirect:typeList?flag=1";
    }

    /**
     * 项目修改（查看页面）
     */
    @GetMapping("/typeEdit")
    public String projectEdit(HttpServletRequest request,int id){
        request.setAttribute("flag",1);
        request.setAttribute("projects",projectService.getId(id));
        return "/admin/type_edit.jsp";
    }

    /**
     * 实现修改
     */
    @PostMapping("/typeUpdate")
    public String projectUpdate(Projects projects,MultipartFile file,
                            @RequestParam(required = false,defaultValue = "1")int page) throws Exception {
        if (Objects.nonNull(file) && !file.isEmpty()){
            projects.setXmimg(UploadUtil.upload(file));
        }
        projectService.update(projects);
        return "redirect:typeList?flag=1&page"+page;
    }

}
